package com.apache.funds.service.plugins;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.database.constant.Validator;
import com.apache.funds.entity.SvmUser;
import com.apache.funds.manager.SvmUserManager;
import com.apache.funds.util.Constants;

/**  
 * 资金账户口令更换
 * @author xhe 创建时间：2015-12-08         
 */
public class FundsAccountPassPlugin implements PluginConnector {
	private Logger log = LoggerFactory.getLogger(getClass());

	private SvmUserManager svmUserManager;

	@Override
	public Object execute(ParamsVo arg0) throws Exception {
		ResultEntity entity = new ResultEntity();
		entity.setResult("true");
		entity.setMessage("操作失败");
		entity.setEntity("false");

		log.info("资金账户口令更换操作....");

		Map<String, String> prarmMap = arg0.getParams();
		//校验参数
		String userEname = prarmMap.get("userEname");
		String passold = prarmMap.get("passold");
		String passnew = prarmMap.get("passnew");
		if (Validator.isNull(userEname) || Validator.isNull(passold)) {
			entity.setMessage(Constants.Msg.F006+"账户或者口令不能为空");
			entity.setEntity("false");
			return entity;
		}

		ParamsVo<SvmUser> vo = new ParamsVo<SvmUser>();
		vo.setParams("userName", userEname);
		vo.setMethodKey("ByUserName");
		SvmUser user = (SvmUser) svmUserManager.execute(vo);
		if (null == user) {
			entity.setMessage(Constants.Msg.F004);
			entity.setEntity("false");
			return entity;
		}
		String userPass = user.getPassWord();

		//密码口令修改1层校验：（账号和旧口令对应校验）
		if (!passold.equals(userPass)) {
			entity.setMessage(Constants.Msg.F011);
			entity.setEntity("false");
			return entity;
		}

		//密码口令修改2层校验：后续补充邮箱，或者短信验证？

		//修改口令
		user.setPassWord(passnew);
		ParamsVo<SvmUser> voSave = new ParamsVo<SvmUser>();
		voSave.setObj(user);
		svmUserManager.editInfo(voSave);

		entity.setEntity("true");
		entity.setMessage(Constants.Msg.F000 + ",更新完成！");
		return entity;
	}

	public void setSvmUserManager(SvmUserManager svmUserManager) {
		this.svmUserManager = svmUserManager;
	}
}
